import * as ww from "@wecom/jssdk";
import { getConfigSignature as getConfigSignatureApi } from "@/api/modules/ww";

async function getConfigSignature() {
  const URL = location.href.split("#")[0];
  const { data } = await getConfigSignatureApi({
    url: URL
  });
  const { timestamp, nonceStr, signature } = data;
  return { timestamp, nonceStr, signature };
}

export const registerWW = () => {
  if (ww.env.isWeChat || ww.env.isWeCom) {
    ww.register({
      corpId: "ww569d0c17adad7515", // 必填，当前用户企业所属企业ID
      agentId: "1000064", // 必填，当前应用的AgentID
      jsApiList: ["openDefaultBrowser", "closeWindow"], // 必填，需要使用的JSAPI列表
      getConfigSignature, // 必填，根据url生成企业签名的回调函数
      onConfigComplete: () => {
        ww.openDefaultBrowser({
          url: location.href
        });
        ww.closeWindow();
      }
    });
  }
};
